//
//  QPointsObtainVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/3/20.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QPointsObtainVC.h"
#import "QPointsTypeCell.h"
#import "QPointListModel.h"
#import "QPointDetailModel.h"
@interface QPointsObtainVC ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _index;
    NSMutableArray *_dataArr;
    QPointListModel *listModel;
}
@property (nonatomic,strong)UITableView *tbView;

@end

@implementation QPointsObtainVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
    _dataArr = [NSMutableArray array];
    [self requestData];
    
    WS(wSelf);
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [wSelf requestData];
    }];
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [wSelf requestMoreData];
    }];

}

- (void)requestData{
    _index = 1;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getIntegralOrderInfoAction"];
    NSDictionary *para= [QParamService getIntegralOrderInfoActionWithOper_type:@"2" Start:start PageSize:KpageSize];
    
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            listModel =[QPointListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:listModel.list];
            
            if(_dataArr.count == 0){
                [QViewUtility showNoRecordViewInView:_tbView Title:@""];
            }else{
                if(listModel.list.count < 10){
                    [_tbView.mj_footer noticeNoMoreData];
                }
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}


- (void)requestMoreData{
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getIntegralOrderInfoAction"];
    NSDictionary *para= [QParamService getIntegralOrderInfoActionWithOper_type:@"2" Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
             listModel = [QPointListModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:listModel.list];
            if(listModel.list.count < 10){
                [_tbView.mj_footer noticeNoMoreData];

            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}


- (void)createUI{
    [self.view addSubview:self.tbView];
    [self.tbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo (@0);
    }];
    
    self.tbView.showsVerticalScrollIndicator = NO;

    
    [self.tbView registerNib:[UINib nibWithNibName:@"QPointsTypeCell" bundle:nil] forCellReuseIdentifier:@"QPointsTypeCell"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiAction:) name:kNotificationscrollsToTop object:nil];

}

- (void)notifiAction:(NSNotification *)notification{
    NSString *topStr = [notification.userInfo objectForKey:kTopKey];
    
    if([topStr isEqualToString:@"0"]){
        _tbView.scrollsToTop = YES;
    }else{
        _tbView.scrollsToTop = NO;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QPointsTypeCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QPointsTypeCell"];
    QPointDetailModel * model = [_dataArr objectAtIndex:indexPath.row];
    model.yangkai = @"2";
    model.curr_date = listModel.curr_date;
    cell.model = model;
    return cell;
}
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
